package com.jiangnan.viewmodel

import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.jiangnan.entity.LoginInfo
import com.jiangnan.entity.Res
import com.jiangnan.entity.User
import com.jiangnan.entity.UserBorrowCertificate
import com.jiangnan.repository.UserRepository
import kotlinx.coroutines.launch
import okhttp3.MultipartBody

class UserViewModel : ViewModel() {

    private var repository = UserRepository()

    // LiveData 更新用户信息接口的返回信息
    private var _updateUserInfoResponse = MutableLiveData<Res<String>>()
    val updateUserInfoResponse: MutableLiveData<Res<String>> get() = _updateUserInfoResponse

    // 更新用户头像
    private var _avatarUrl = MutableLiveData<String>()
    val avatarUrl: MutableLiveData<String> get() = _avatarUrl

    private val _user = MutableLiveData<User?>()
    val user: LiveData<User?> = _user

    private var _certificate = MutableLiveData<UserBorrowCertificate>()
    val userCertificate: LiveData<UserBorrowCertificate> get() = _certificate

    // 打印提示信息
    private var _toastMessage = MutableLiveData<String>()
    val toastMessage: MutableLiveData<String> get() = _toastMessage


    fun loadUserCertificate(userId: Long) {
        viewModelScope.launch {
            try {
                val res: Res<UserBorrowCertificate> = repository.fetchUserCertificate(userId)
                if (res.code == 1) {
                    _certificate.value = res.data!!
                } else {
                    _toastMessage.value = res.msg
                }
            } catch (e: Exception) {
                _toastMessage.value = e.message
                e.printStackTrace()
            }
        }
    }


    fun updateUserInfo(userId: Int, userInfo: LoginInfo) {
        viewModelScope.launch {
            val res = repository.updateUserInfo(userId, userInfo)
            try {
                if (res.code == 1) {
                    _updateUserInfoResponse.value = res
                    loadUser(userId.toLong())
                    _toastMessage.postValue("更新成功")
                } else {
                    _updateUserInfoResponse.value = Res(0, "更新失败", null)
                    _toastMessage.postValue("更新失败")
                }
            } catch (e: Exception) {
                e.printStackTrace()
                _toastMessage.postValue("更新失败")
            }
        }
    }

    /**
     * 上传用户头像的方法
     */
    fun uploadAvatarImage(filePart: MultipartBody.Part) {
        viewModelScope.launch {
            try {
                val response = repository.uploadAvatarImage(filePart)
                if (response.isSuccessful) {
                    _toastMessage.postValue("上传失败")
                    _avatarUrl.postValue(response.body()!!.data!!)
                }
                _toastMessage.postValue("上传成功")
            } catch (e: Exception) {
                e.printStackTrace()
                _toastMessage.postValue("上传失败")
            }
        }
    }

    fun loadUser(userId: Long) {
        viewModelScope.launch {
            try {
                val res: Res<User> = repository.fetchUser(userId)
                if (res.code == 1 && res.data != null) {
                    _user.value = res.data
                } else {
                    _toastMessage.value = res.msg
                }
            } catch (e: Exception) {
                _toastMessage.value = e.message
            }
        }
    }


}